package member.order.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.util.*;
import m.system.RuntimeData;
import m.system.exception.MException;
import m.system.util.DateUtil;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import member.base.model.UserInfo;
import member.base.service.PageService;
import member.base.util.PowerUtil;
import member.order.model.MemberRecharge;
import member.order.model.OrderInfo;
import member.order.model.OrderModel;
import member.order.util.MoneyRecordUtil;
import member.order.util.OrderCollectUtil;
import member.order.util.OrderNoUtil;

import java.util.Date;

@ActionRequest(name = "action/order/memberRecharge",description = "会员充值订单")
public class MemberRechargeAction extends OrderBaseAction<MemberRecharge> {

    private MemberRecharge model;

    @RequestMethod(description = "我的列表")
    public JSONMessage getMyList() throws Exception {
        ModelQueryList queryList=toQueryList(MemberRecharge.class,"*","member.*","member.level.*");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        queryList.addCondition(QueryCondition.eq("inputUser.oid", PowerUtil.getUser().getOid()));
        setMemberPhoneNumExpression(queryList);
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "确认列表")
    public JSONMessage getCheckList() throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerStore);
        ModelQueryList queryList=toQueryList(MemberRecharge.class,"*","member.*","member.level.*");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        queryList.addCondition(QueryCondition.eq("inputStore.oid", PowerUtil.getStore().getOid()));
        queryList.addCondition(QueryCondition.eq("status", OrderModel.StatusConfirm));
        setMemberPhoneNumExpression(queryList);
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "店铺列表")
    public JSONMessage getStoreList() throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerStore);
        ModelQueryList queryList=toQueryList(MemberRecharge.class,"*","member.*","member.level.*");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        queryList.addCondition(QueryCondition.eq("inputStore.oid", PowerUtil.getStore().getOid()));
        queryList.addCondition(QueryCondition.eq("status", OrderModel.StatusDone));
        setMemberPhoneNumExpression(queryList);
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "商家列表")
    public JSONMessage getBusinessList() throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerManage);
        ModelQueryList queryList=toQueryList(MemberRecharge.class,"*","member.*","member.level.*");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        setMemberPhoneNumExpression(queryList);
        return JSONMessage.success("查询成功").push("page", ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "获取信息")
    public JSONMessage getInfo(@Param("oid") String oid) throws Exception {
        MemberRecharge model;
        if(StringUtil.isSpace(oid)){
            model=new MemberRecharge();
            fillInitProperty(model);
            model.setAccountDate(new Date());
        }else{
            ModelQueryList queryList=ModelQueryList.instance(MemberRecharge.class,
                new String[]{"*","member.*","member.level.*",
                    "inputUser.realName","inputUser.phoneNum","serveUser.realName","serveUser.phoneNum","confirmUser.realName","confirmUser.phoneNum",
                    "order.*"},
                null,QueryCondition.eq("oid",oid)
            );
            setMemberPhoneNumExpression(queryList);
            model=queryList.getModel();
        }
        PowerUtil.verifyBusiness(model.getBusiness());
        return JSONMessage.success("查询成功")
            .push("model",model)
            .push("editable", OrderInfo.StatusSave.equals(model.getStatus())
                &&PowerUtil.hasPower(model.getInputUser()))
            .push("confirmable",OrderInfo.StatusConfirm.equals(model.getStatus())
                &&PowerUtil.hasPower(UserInfo.PowerStore)
                &&PowerUtil.hasPower(model.getInputStore()));
    }
    @Override
    public MemberRecharge saveOrder() throws Exception {
        model.setBusiness(PowerUtil.verifyBusiness(model.getBusiness()));
        ModelQueryCheck.init(model)
            .checkNotNull("member.oid","请选择会员")
            .checkNotNull("serveUser.oid","请选择服务员工")
            .checkNotNull("type","请选择付款方式")
            .checkNotNull("payAmount","请输入付款金额")
            .checkNotNull("accountDate","请选择账期")
            .doCheck(false,true);
        model.setSurplusPayAmount(model.getPayAmount());
        model.setSurplusGiveAmount(model.getGiveAmount());
        if(ObjectUtil.notEmptyOid(model)){
            ModelCheckUtil.equals(model,new String[]{"status","inputUser.oid"},new Object[]{OrderModel.StatusSave,PowerUtil.getUser().getOid()},"状态错误");
        }else{
            fillInitProperty(model);
            model.setOrderNo(OrderNoUtil.newOrderNo(MemberRecharge.class,model.getBusiness().getOid(),"C"+ DateUtil.format(model.getInputDate(),"yyyyMM").substring(2)));
        }
        ModelUpdateUtil.saveModel(model,"member.oid","serveUser.oid","type","payAmount","giveAmount","givePoint","accountDate","remark");
        return model;
    }

    @Override
    public void deleteOrder(MemberRecharge model) throws Exception {
        ModelCheckUtil.equals(model,new String[]{"status","inputUser.oid"},new Object[]{OrderModel.StatusSave,PowerUtil.getUser().getOid()},"状态错误");
        ModelUpdateUtil.deleteModel(model);
    }

    @Override
    public void backAfter(MemberRecharge model) throws Exception {
        //清除首页统计缓存
        RuntimeData.getService(PageService.class).clearRecharge(model.getInputStore().getOid());
    }

    @Override
    public void submitAfter(MemberRecharge model) throws Exception {
        //清除首页统计缓存
        RuntimeData.getService(PageService.class).clearRecharge(model.getInputStore().getOid());
    }

    @Override
    public void confirmOrder() throws Exception {
        String remark=model.getRemark();
        Date accountDate=model.getAccountDate();
        if(null==accountDate) throw new MException("请选择账期");
        model= ModelQueryList.getModel(model,"*");
        if(!OrderModel.StatusConfirm.equals(model.getStatus())) throw new MException("状态错误");
        model.setConfirmUser(PowerUtil.getUser());
        model.setConfirmDate(new Date());
        model.setStatus(OrderModel.StatusDone);
        model.setAccountDate(accountDate);
        model.setRemark(remark);
        ModelUpdateUtil.updateModel(model,"confirmUser.oid","confirmDate","status","accountDate","remark");
        //添加充值记录
        MoneyRecordUtil.addRecharge(model);
        //更新订单汇总
        OrderCollectUtil.add(model.getInputStore().getOid(),model.getAccountDate());
        OrderCollectUtil.start();
        //清除首页统计缓存
        RuntimeData.getService(PageService.class).clearRecharge(model.getInputStore().getOid());
    }

    @Override
    public MemberRecharge getModel() {
        return model;
    }

    public void setModel(MemberRecharge model) {
        this.model = model;
    }
}
